`
lrwxrwxrwx 1 root root 7 Mar 10 08:43 bin -> usr/bin
lrwxrwxrwx 1 root root 8 Mar 10 08:43 sbin -> usr/sbin
Listing 1-16
Piping a commands output as input to another command
We used ls to print the content of the root directory into the
standard output stream, then used pipe (|) to send it as input to the
grep command, which filtered out any lines containing the word
bin.
Positional Arguments
Bash scripts can take positional arguments (also called
parameters) passed on the command line. Arguments are especially
useful, for example, when you want to develop a program that
modifies its behavior based on some input passed to it by another
program or user. Arguments can also change features of the script
such as the output format and how verbose it will be during runtime.
For example, imagine you develop an exploit and send it to a few
colleagues, each of whom will use it against a different IP address.
Instead of writing a script and asking the user to modify it with their
network information, you can write it to take an IP address argument
and then act against this input to avoid having to modify the source
code in each case.
A bash script can access arguments passed to it on the command
line using the variables $1, $2, and so on. The number represents
the order in which the argument was entered. To illustrate this, the
following script takes in an argument (an IP address or domain
name) and performs a ping test against it using the ping utility.
Save this file as ping_with_arguments.sh:
#!/bin/bash
# This script will ping any address provided as an argument.
SCRIPT_NAME="${0}"
TARGET="${1}"
echo "Running the script ${SCRIPT_NAME}..."
echo "Pinging the target: ${TARGET}..."
ping "${TARGET}"
Listing 1-17
A pinger command
Black Hat Bash (Early Access) © 2023 by Dolev Farhi and Nick Aleks